package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkBackVisitTemplateModule;
import com.zb.entity.ZjWorkModule;
import com.zb.extend.WorkBackVisitTemplateModuleExtend;
import com.zb.mapper.ZjWorkBackVisitTemplateModuleMapper;
import com.zb.service.ZjWorkBackVisitTemplateModuleService;
import com.zb.service.ZjWorkModuleService;
import com.zb.vo.TemplateModuleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单回访模板模块关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-08
 */
@Service
public class ZjWorkBackVisitTemplateModuleServiceImpl extends ServiceImpl<ZjWorkBackVisitTemplateModuleMapper, ZjWorkBackVisitTemplateModule> implements ZjWorkBackVisitTemplateModuleService {
    @Resource
    private ZjWorkModuleService workModuleService;

    // 保存回访应用模块
    @Override
    public void save(Long custAccId, Long workBackVisitTemplateId, List<TemplateModuleVo> templateModuleIds, List<Long> delTemplateModuleIds) {
        if (null != delTemplateModuleIds && !delTemplateModuleIds.isEmpty()) {
            // todo 查询是否在工单回访表中使用
//            QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
//            wrapper.select("module_id");
//            wrapper.in("id", delTemplateModuleIds);
//            List<Long> moduleIds = this.list(wrapper).stream().map(ZjWorkBackVisitTemplateModule::getModuleId).distinct().toList();
//
//            List<ZjWorkModule> modules = workModuleService.getPartInfoByIds(moduleIds);
//            Assert.isTrue(modules.isEmpty(), "应用模块[" + modules.stream().map(ZjWorkModule::getName).collect(Collectors.joining("、")) + "]已被使用中，不可删除");

            this.removeByIds(delTemplateModuleIds);
        }
        if (null == templateModuleIds || templateModuleIds.isEmpty()) {
            return;
        }
        List<ZjWorkBackVisitTemplateModule> adds = new ArrayList<>();
        List<ZjWorkBackVisitTemplateModule> updates = new ArrayList<>();
        ZjWorkBackVisitTemplateModule module = null;
        for (TemplateModuleVo vo : templateModuleIds) {
            module = new ZjWorkBackVisitTemplateModule();
            BeanUtil.copyProperties(vo, module);
            if (null != module.getId()) {
                updates.add(module);
                continue;
            }
            module.setId(IdWorker.getId());
            module.setCustAccId(custAccId);
            module.setWorkBackVisitTemplateId(workBackVisitTemplateId);
            adds.add(module);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据回访ID获取应用模块
    @Override
    public List<WorkBackVisitTemplateModuleExtend> getByWorkBackVisitTemplateIds(List<Long> workBackVisitTemplateIds) {
        if (null == workBackVisitTemplateIds || workBackVisitTemplateIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_back_visit_template_id", "module_Id");
        wrapper.in("work_back_visit_template_id", workBackVisitTemplateIds);
        List<ZjWorkBackVisitTemplateModule> templateModules = this.list(wrapper);
        List<Long> moduleIds = templateModules.stream().map(ZjWorkBackVisitTemplateModule::getModuleId).distinct().toList();

        List<ZjWorkModule> modules = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(moduleIds)) {
            modules.addAll(workModuleService.getPartInfoByIds(moduleIds));
        }

        return templateModules.stream().map(tem -> {
            WorkBackVisitTemplateModuleExtend extend = new WorkBackVisitTemplateModuleExtend();
            BeanUtil.copyProperties(tem, extend);
            extend.setModuleName(modules.stream().filter(f -> Objects.equals(f.getId(), extend.getModuleId())).map(ZjWorkModule::getName).collect(Collectors.joining("、")));
            return extend;
        }).toList();
    }

    // 根据回访ID获取应用模块
    @Override
    public List<ZjWorkBackVisitTemplateModule> getByWorkBackVisitTemplateId(Long workBackVisitTemplateId) {
        QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_back_visit_template_id", "module_Id");
        wrapper.eq("work_back_visit_template_id", workBackVisitTemplateId);
        return this.list(wrapper);
    }

    // 根据回访模板ID删除
    @Override
    public void deleteBatchByWorkBackVisitTemplateIds(List<Long> workBackVisitTemplateIds) {
        if (null == workBackVisitTemplateIds || workBackVisitTemplateIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
        wrapper.in("work_back_visit_template_id", workBackVisitTemplateIds);
        this.remove(wrapper);
    }

    // 根据模块ID获取回访模板应用模块
    @Override
    public List<ZjWorkBackVisitTemplateModule> getByModuleId(Long moduleId) {
        QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_back_visit_template_id", "module_Id");
        wrapper.eq("module_Id", moduleId);
        return this.list(wrapper);
    }

    // 根据模块ID集合获取回访模板应用模块
    @Override
    public List<ZjWorkBackVisitTemplateModule> getByModuleIds(List<Long> moduleIds) {
        if (null == moduleIds || moduleIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkBackVisitTemplateModule> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_back_visit_template_id", "module_Id");
        wrapper.in("module_Id", moduleIds);
        return this.list(wrapper);
    }
}
